<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #inner {
            position: absolute;
            top: 50px;
            left: 50px;
            height: 200px;
            width: 200px;
            background-color: red;
        }
    </style>
</head>
<body>

<button id="start">开始</button>
<button id="back">回放</button>
<div id="inner">
</div>
</body>
<script>
    var div = document.querySelector("#inner");
    var start = document.querySelector("#start");
    var back = document.querySelector("#back");
    var isStart = false;
    var moveX = [];
    var moveY = [];

    start.onmousedown = function (e) {
        var evt = window.event || e;
        evt.stopPropagation();
        isStart = true;
        moveX = [];
        moveY = [];
    }
    back.onmousedown = function (e) {
        var evt = window.event || e;
        evt.stopPropagation();
        var maxlength = moveX.length;
        var timer = setInterval(function () {
            var index = maxlength--;
            if (index < 0) {
                clearInterval(timer);
                return false;
            }
            div.style.left = moveX[index] + "px";
            div.style.top = moveY[index] + "px";
        }, 30);
    }


    div.onmousedown = function (e) { //先鼠标按下 获取相对位置
        var evt = window.event || e;
        if (isStart) {

            var blackLineX = evt.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft);
            var redLineX = div.offsetLeft;

            var blackLineY = evt.clientY + (document.documentElement.scrollTop || document.body.scrollTop);
            var redLineY = div.offsetTop;

            var disX = blackLineX - redLineX;
            var disY = blackLineY - redLineY;

            document.onmousemove = function (e) {  //按下移动
                var evt = window.event || e;
                var x = evt.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - disX;
                var y = evt.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - disY;

                div.style.left = x + "px";
                div.style.top = y + "px";
                moveX.push(x);
                moveY.push(y);

            }
            div.onmouseup = function () { //取消移动
                document.onmousemove = null;
            }
        }
    }
</script>
</html>